package com.sinven.conf;

import com.sinven.interceptor.UserInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.FormContentFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import javax.annotation.Resource;

@Configuration
@Slf4j
public class SystemConfig extends WebMvcConfigurationSupport {

    @Resource
    CmsProperties cmsProps;

    @Autowired
    private UserInterceptor userInterceptor;
    @Override
    protected void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/manage/").setViewName("redirect:/manage/login.html");
        registry.addViewController("/manage").setViewName("redirect:/manage/login.html");
        super.addViewControllers(registry);
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.debug("映射的图片文件夹为:{}",cmsProps.getUploadBaseDirectory());
        registry.addResourceHandler("/"+cmsProps.getUploadBaseFolder()+"/**").
                addResourceLocations(cmsProps.getUploadBaseDirectory());
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        super.addResourceHandlers(registry);
    }

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        String[] excludePatterns = new String[]{"/manage/","/manage/login.html","/manage/users"};
        registry.addInterceptor(userInterceptor).addPathPatterns("/manage/**").excludePathPatterns(excludePatterns);
        super.addInterceptors(registry);
    }

    @Bean
    public FormContentFilter httpPutFormContentFilter() {
        return new FormContentFilter();
    }
}
